package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result page(QueryPageBean queryPageBean){
        log.info("[检查组项-分页查询]data:{}",queryPageBean);
        PageResult pageResult = checkGroupService.page(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS,pageResult);
    }

    /**
     * 新增
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup ,Integer[] checkitemIds){
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据groupId查询ItemId
     * @param id
     * @return
     */
    @GetMapping("findCheckItemIdsByCheckGroupId")
    public Result select(Integer id){
        List<Integer> ids = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,ids);
    }

    /**
     * 根据groupId查询信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result selectById(Integer id){
        CheckGroup checkGroup = checkGroupService.fingById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }

    /**
     * 修改
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup ,Integer[] checkitemIds){
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result delete(Integer id){
        checkGroupService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroups);
    }
}
